package com.android.equipment.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.equipment.R;
import com.android.equipment.base.BaseFragment;
import com.android.equipment.bean.VersionBean;
import com.android.equipment.dao.operate.DaoData;
import com.android.equipment.dao.operate.DaoDeviceNode;
import com.android.equipment.dao.operate.DaoHistoryAlam;
import com.android.equipment.dao.operate.DaoRealTimeAlam;
import com.android.equipment.dao.operate.DaoUser;
import com.android.equipment.dao.operate.DaoWorkDetail;
import com.android.equipment.databinding.FragmentUserBinding;
import com.android.equipment.net.DialogCallback;
import com.android.equipment.net.OkUtil;
import com.android.equipment.net.ResponseBean;
import com.android.equipment.ui.device.DeviceManagerActivity;
import com.android.equipment.ui.model.DeviceModelManagerActivity;
import com.android.equipment.ui.MainActivity;
import com.android.equipment.ui.group.TeamGroupActivity;
import com.android.equipment.ui.people.PeopleManagerActivity;
import com.android.equipment.ui.dialog.UpdatePassDialog;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.lxj.xpopup.XPopup;
import com.lzy.okgo.model.Response;

import java.util.Objects;

public class UserFragment extends BaseFragment {
    FragmentUserBinding binding;
    private long versionCode;

    @Override
    protected View setLayoutContent(LayoutInflater inflater, ViewGroup container) {
        binding = FragmentUserBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    protected void initView(Bundle savedInstanceState, View view) {
        String userAccount = DaoUser.getInstance().getUserAccount();
        if (!StringUtils.isEmpty(userAccount)) {
            binding.tvUserAccount.setText(userAccount);
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void initData() {

        try {
            PackageManager packageManager = requireContext().getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(requireContext().getPackageName(), 0);
            String versionName = packageInfo.versionName;
            versionCode = packageInfo.versionCode;
            binding.tvCurrentVersion.setText("V"+versionName);
        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    protected void initEvent() {
        super.initEvent();
        binding.llUpdatePass.setOnClickListener(v -> {
            new XPopup.Builder(getContext())
                    .dismissOnBackPressed(false)
                    .dismissOnTouchOutside(false)
                    .asCustom(new UpdatePassDialog(getContext()))
                    .show();
        });

        binding.llPeopleManager.setOnClickListener(v -> {
            ((MainActivity) requireActivity()).mSwipeBackHelper.forward(PeopleManagerActivity.class);
        });

        binding.llDeviceManager.setOnClickListener(v -> {
            ((MainActivity) requireActivity()).mSwipeBackHelper.forward(DeviceManagerActivity.class);
        });

        binding.llModelManager.setOnClickListener(v -> {
            ((MainActivity) requireActivity()).mSwipeBackHelper.forward(DeviceModelManagerActivity.class);
        });
        binding.llPeopleGroup.setOnClickListener(v -> {
            ((MainActivity) requireActivity()).mSwipeBackHelper.forward(TeamGroupActivity.class);
        });

        binding.llVersion.setOnClickListener(V -> {
            OkUtil.getRequest("https://api.hdiyun.com/api-user/config-app/getByKey?key=bLJg8hG9", new DialogCallback<ResponseBean<VersionBean>>(mContext) {
                @Override
                public void onSuccess(Response<ResponseBean<VersionBean>> response) {
                    VersionBean data = response.body().getData();
                    if (data.getVersionCode() > versionCode){
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(data.getAndroidUrl()));
                        Intent chooser = Intent.createChooser(intent, "Choose a browser");
                        startActivity(chooser);
                    }else {
                        ToastUtils.showShort("当前已经是最新版本");
                    }
                }
            });
        });

        binding.btnDevelop.setOnClickListener(v ->{
            //删除所有历史报警
//            DaoHistoryAlam.getInstance().deleteAllHistory();
//            //删除所有实时报警
//            DaoRealTimeAlam.getInstance().deleteAllRealTime();
//            //删除所有实时数据
//            DaoData.getInstance().deleteAllData();
//            //删除所有的设备节点数据
//            DaoDeviceNode.getInstance().deleteAllDeviceNode();

            DaoWorkDetail.getInstance().deleteAllWorkDetail();
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        ImmersionBar.with(this).statusBarDarkFont(true).init();
    }


}
